package com.example.androidkotlinmvvm

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.androidkotlinmvvm.databinding.ActivityXmlTestBinding
import com.example.androidkotlinmvvm.mvvm.User2
import com.example.androidkotlinmvvm.xml.InfoListViewModel
import com.example.androidkotlinmvvm.xml.MyAdapter
import com.example.androidkotlinmvvm.xml.MyRepository

class XmlTestActivity : AppCompatActivity() {

    private lateinit var binding: ActivityXmlTestBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityXmlTestBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.rv.layoutManager = LinearLayoutManager(this)
        var myList= mutableListOf<User2>()
        val adapter = MyAdapter(myList)
        binding.rv.adapter = adapter
        val viewModel = ViewModelProvider(this).get(InfoListViewModel::class.java)
        viewModel.fetchUser()
        viewModel.user.observe(this, Observer { user ->
//            Log.e("rrrrrrrrrr",user.toString())
            binding.viewModel=viewModel
            myList= user.data!!
            adapter.setData(myList)
            adapter.notifyDataSetChanged()
        })
    }
}